/*
 * AddTema.java
 *
 * Created on 2 de diciembre de 2008, 11:31
 */
package Vistas;

import Dominio.CtrlDominio;
import java.util.Vector;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

/**
 *
 * @author  sandra
 */
public class AddAutoTema extends javax.swing.JFrame {

    private CtrlDominio CD = CtrlDominio.getInstance();
    //private Vector<String> temasPos=new Vector<String>(0, 1);
    private Vector<String> temasSeg=new Vector<String>(0, 1);
    private Vector<String> temas = new Vector<String>(0, 1);
    private static String[] cabecerasTablaTema = {"Lista Temas"};
    private static String[] cabeceraTemaNoticia={"Temas Noticia"};
    private AuxNoticia not;
    private NoticiaAutomatica N;
    private String secc;
    /** Creates new form AddTema */
    /* public AddTema(int i, AuxNotManual auxx) {
    initComponents();
    }*/
    
    public AddAutoTema(AuxNoticia xn,NoticiaAutomatica na,String seccion) {
        initComponents();
        not=xn;
        N=na;
        secc=seccion;
        this.setTitle("A\u00f1adir Temas");
        this.setLocationRelativeTo(null);
        temas = CD.listarTemasSeccion(secc);
        if (temas.size() == 0) {
            JOptionPane.showMessageDialog(this, "No es posible mostrar los temas, porque no hay ning\u00fan tema en el diccionario", "No hay temas en el diccionario", JOptionPane.INFORMATION_MESSAGE);
        } else {
            String[][] aux2 = new String[temas.size()][1];
            for (int i = 0; i < temas.size(); i++) {
                aux2[i][0] = temas.get(i);
            }
            jTemas.setModel(new javax.swing.table.DefaultTableModel(aux2, cabecerasTablaTema));
            jButton1.setEnabled(false);
            String fs = System.getProperty("file.separator");
            ImageIcon ic = new ImageIcon(System.getProperty("user.dir") + fs + "src" + fs + "Image" + fs + "add.png");
            jAddTema.setIcon(ic);
            jAddList.setIcon(ic);
            ImageIcon ic2 = new ImageIcon(System.getProperty("user.dir") + fs + "src" + fs + "Image" + fs + "remove.png");
            jButton1.setIcon(ic);
            ImageIcon ic3 = new ImageIcon(System.getProperty("user.dir") + fs + "src" + fs + "Image" + fs + "return.png");
            jButton3.setIcon(ic3);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTemas = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        jNuevoTema = new javax.swing.JTextField();
        jAddTema = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jAddList = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTemas1 = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTemas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null},
                {null},
                {null},
                {null}
            },
            new String [] {
                "Lista Temas"
            }
        ));
        jScrollPane1.setViewportView(jTemas);
        jTemas.getColumnModel().getColumn(0).setResizable(false);

        jAddTema.setText("Añadir Nuevo Tema");
        jAddTema.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jAddTemaMouseClicked(evt);
            }
        });

        jButton3.setText("Volver");
        jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton3MouseClicked(evt);
            }
        });

        jAddList.setText("Añadir Tema de la lista");
        jAddList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jAddListMouseClicked(evt);
            }
        });

        jButton1.setText("Quitar Tema");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, 215, Short.MAX_VALUE)
                    .addComponent(jAddList, javax.swing.GroupLayout.DEFAULT_SIZE, 215, Short.MAX_VALUE)
                    .addComponent(jNuevoTema, javax.swing.GroupLayout.DEFAULT_SIZE, 215, Short.MAX_VALUE)
                    .addComponent(jAddTema, javax.swing.GroupLayout.DEFAULT_SIZE, 215, Short.MAX_VALUE)
                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 215, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jNuevoTema, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jAddTema)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 50, Short.MAX_VALUE)
                .addComponent(jButton3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jAddList)
                .addGap(14, 14, 14))
        );

        jTemas1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null},
                {null},
                {null}
            },
            new String [] {
                "Temas"
            }
        ));
        jTemas1.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jTemas1FocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTemas1FocusLost(evt);
            }
        });
        jScrollPane2.setViewportView(jTemas1);
        jTemas1.getColumnModel().getColumn(0).setResizable(false);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 249, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 249, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 195, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(123, 123, 123))))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(50, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 311, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void jAddListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jAddListMouseClicked
        if(jAddList.isEnabled()){
        int fila = jTemas.getSelectedRow();
        if (fila == -1) {
            JOptionPane.showMessageDialog(this, "Selecciona un tema para consultar", "No se ha seleccionado un tema de la lista", JOptionPane.INFORMATION_MESSAGE);
        } else {
            String tema = (String) jTemas.getValueAt(fila, 0);
            
            if (temasSeg.contains(tema)) {
                JOptionPane.showMessageDialog(this, "El tema ya est\u00e1 a\u00f1adido a la noticia", "Tema a\u00f1adido", JOptionPane.INFORMATION_MESSAGE);
            }
            else {
                temasSeg.add(tema);
                actualizarTabla();
            }
            if(temasSeg.size()==3){
                jAddTema.setEnabled(false);
                jAddList.setEnabled(false);
            }
        }
        }
    }//GEN-LAST:event_jAddListMouseClicked

    private void jAddTemaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jAddTemaMouseClicked
        if(jAddTema.isEnabled()){
        String tema = jNuevoTema.getText();
        jNuevoTema.setText("");
        if (tema.isEmpty()) {
            JOptionPane.showMessageDialog(this, "No hay ning\u00fan tema nuevo para a\u00f2adir, int\u00e9ntelo de nuevo.", "No hay tema nuevo", JOptionPane.INFORMATION_MESSAGE);
        } else {
            if (CD.estaDiccionarioTemas(tema)) {
                JOptionPane.showMessageDialog(this, "El tema que intentas a\u00f2adir ya est\u00e1 en el diccionario.", "Ya existe el tema", JOptionPane.INFORMATION_MESSAGE);
            }
            else if(temasSeg.contains(tema)){
                JOptionPane.showMessageDialog(this, "El tema que intentas a\u00f2adir ya se encuentra en la lista", "Ya existe el tema", JOptionPane.INFORMATION_MESSAGE);
            }
            else {
                temasSeg.add(tema);
                this.actualizarTabla();              
            }
            if(temasSeg.size()==3){
                jAddTema.setEnabled(false);
                jAddList.setEnabled(false);
            }
        }
        }
}//GEN-LAST:event_jAddTemaMouseClicked

    private void jButton3MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton3MouseClicked
        N.activar();
        not.temas=temasSeg;
        N.actualizarTabla();
        this.dispose();
    }//GEN-LAST:event_jButton3MouseClicked

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
        if(jButton1.isEnabled()){
          temasSeg.remove(jTemas1.getSelectedRow());
          actualizarTabla();
          jAddTema.setEnabled(true);
          jAddList.setEnabled(true);
        }
    }//GEN-LAST:event_jButton1MouseClicked

    private void jTemas1FocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTemas1FocusGained
        if(jTemas1.getSelectedRow()>-1)
            jButton1.setEnabled(true);
    }//GEN-LAST:event_jTemas1FocusGained

    private void jTemas1FocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTemas1FocusLost
        jButton1.setEnabled(false);
    }//GEN-LAST:event_jTemas1FocusLost
    
    private void actualizarTabla(){
        String[][] tab= new String[temasSeg.size()][1];
        for(int i=0;i<temasSeg.size();i++){
            tab[i][0]=temasSeg.get(i);
        }
        jTemas1.setModel(new javax.swing.table.DefaultTableModel(tab, cabeceraTemaNoticia));
    }
    /**
     * @param args the command line arguments
     */
    /*   public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new AddTema().setVisible(true);
    }
    });
    }*/
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jAddList;
    private javax.swing.JButton jAddTema;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton3;
    private javax.swing.JTextField jNuevoTema;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTemas;
    private javax.swing.JTable jTemas1;
    // End of variables declaration//GEN-END:variables
}
